﻿#include "DelayCamerDevice_C.h"
#include "DelayCamerDevice.h"
#include <string>
#include <cstring>

// C接口实现
extern "C" {
	// 设置设备连接回调函数
	DELAYCAMERDEVICE_C_API void SetDevConnCallback(FunBakDevConn devConnFunc)
	{
		DelayCamerDevice::GetInstance()->SetDevConnCallback(devConnFunc);
	}

	// 设置设备状态回调函数
	DELAYCAMERDEVICE_C_API void SetDevStsCallback(FunBakDevSts devStsFunc)
	{
		DelayCamerDevice::GetInstance()->SetDevStsCallback(devStsFunc);
	}

	// 设置校正状态回调函数
	DELAYCAMERDEVICE_C_API void SetCorrStsCallback(FunBakCorrSts corrStsFunc)
	{
		DelayCamerDevice::GetInstance()->SetCorrStsCallback(corrStsFunc);
	}

	// 设置Flash状态回调函数
	DELAYCAMERDEVICE_C_API void SetFlashStsCallback(FunBakFlashSts flashStsFunc)
	{
		DelayCamerDevice::GetInstance()->SetFlashStsCallback(flashStsFunc);
	}

	// 设置AD/IO回调函数
	DELAYCAMERDEVICE_C_API void SetAdIoCallback(FunBakADIO adIoFunc)
	{
		DelayCamerDevice::GetInstance()->SetAdIoCallback(adIoFunc);
	}

	// 设置IP状态回调函数
	DELAYCAMERDEVICE_C_API void SetIPStsCallback(FunBakSetIPSts setIPStsFunc)
	{
		DelayCamerDevice::GetInstance()->SetIPStsCallback(setIPStsFunc);
	}

	// 设置命令同步回调函数
	DELAYCAMERDEVICE_C_API void SetCmdSameCallback(FunBakCmdSame cmdSameFunc)
	{
		DelayCamerDevice::GetInstance()->SetCmdSameCallback(cmdSameFunc);
	}

	// 获取设备IP信息（字符串格式）
	DELAYCAMERDEVICE_C_API void GetIpInfo(char* ip, char* mask, char* gate, int* port)
	{
		DelayCamerDevice* instance = DelayCamerDevice::GetInstance();
		if (instance)
		{
			std::string ipStr, maskStr, gateStr;
			int portValue = 0;
			instance->GetIpInfo(ipStr, maskStr, gateStr, portValue);

			if (ip)
				strncpy_s(ip, 16, ipStr.c_str(), _TRUNCATE);
			if (mask)
				strncpy_s(mask, 16, maskStr.c_str(), _TRUNCATE);
			if (gate)
				strncpy_s(gate, 16, gateStr.c_str(), _TRUNCATE);
			if (port)
				*port = portValue;
		}
	}

	// 设置设备IP信息（字符串格式）
	DELAYCAMERDEVICE_C_API void SetIpInfo(const char* ip, const char* mask, const char* gate, int port)
	{
		DelayCamerDevice* instance = DelayCamerDevice::GetInstance();
		if (instance)
		{
			instance->SetIpInfo(ip ? std::string(ip) : "",
					 mask ? std::string(mask) : "",
					 gate ? std::string(gate) : "",
					 port);
		}
	}

	// 获取设备IP信息（整数格式）
	DELAYCAMERDEVICE_C_API void GetIpInfo2(int* ip, int* mask, int* gate, int* port)
	{
		DelayCamerDevice* instance = DelayCamerDevice::GetInstance();
		if (instance && port)
		{
			int portValue = 0;
			instance->GetIpInfo2(ip, mask, gate, portValue);
			*port = portValue;
		}
	}

	// 设置设备IP信息（整数格式）
	DELAYCAMERDEVICE_C_API void SetIpInfo2(int* ip, int* mask, int* gate, int port)
	{
		DelayCamerDevice* instance = DelayCamerDevice::GetInstance();
		if (instance)
		{
			instance->SetIpInfo2(ip, mask, gate, port);
		}
	}

	// 检查TCP连接是否已打开
	DELAYCAMERDEVICE_C_API int IsOpenTcp()
	{
		DelayCamerDevice* instance = DelayCamerDevice::GetInstance();
		if (instance)
		{
			return instance->IsOpenTcp() ? 1 : 0;
		}
		return 0;
	}

	// 打开TCP连接
	DELAYCAMERDEVICE_C_API int OpenTcp()
	{
		DelayCamerDevice* instance = DelayCamerDevice::GetInstance();
		if (instance)
		{
			return instance->OpenTcp();
		}
		return -1;
	}

	// 关闭TCP连接
	DELAYCAMERDEVICE_C_API int CloseTcp()
	{
		DelayCamerDevice* instance = DelayCamerDevice::GetInstance();
		if (instance)
		{
			return instance->CloseTcp();
		}
		return -1;
	}

	// 获取设备状态
	DELAYCAMERDEVICE_C_API int GetDevSts()
	{
		DelayCamerDevice* instance = DelayCamerDevice::GetInstance();
		if (instance)
		{
			return instance->GetDevSts();
		}
		return -1;
	}

	// 设置校正参数
	DELAYCAMERDEVICE_C_API int SetCorrect()
	{
		DelayCamerDevice* instance = DelayCamerDevice::GetInstance();
		if (instance)
		{
			return instance->SetCorrect();
		}
		return -1;
	}

	// 获取上传信息
	DELAYCAMERDEVICE_C_API int GetUpInfo(int time)
	{
		DelayCamerDevice* instance = DelayCamerDevice::GetInstance();
		if (instance)
		{
			return instance->GetUpInfo(time != 0);
		}
		return -1;
	}

	// 设置Flash参数
	DELAYCAMERDEVICE_C_API int SetFlash(str_Flash* inst)
	{
		DelayCamerDevice* instance = DelayCamerDevice::GetInstance();
		if (instance)
		{
			return instance->SetFlash(inst);
		}
		return -1;
	}

	// 设置设备参数
	DELAYCAMERDEVICE_C_API int SetArgs(str_Args* inst)
	{
		DelayCamerDevice* instance = DelayCamerDevice::GetInstance();
		if (instance)
		{
			return instance->SetArgs(inst);
		}
		return -1;
	}

	// 设置IP参数
	DELAYCAMERDEVICE_C_API int SetIP(str_IP* inst)
	{
		DelayCamerDevice* instance = DelayCamerDevice::GetInstance();
		if (instance)
		{
			return instance->SetIP(inst);
		}
		return -1;
	}
}